"""
@author: yinjiaxing
@file: lianxi-sanle_shoping.py
@time: 2023/3/26 19:48
"""


#####错误多，未能运行，设置变量太多，不想改了
#####错误多，未能运行，设置变量太多，不想改了
#####错误多，未能运行，设置变量太多，不想改了
#####错误多，未能运行，设置变量太多，不想改了
#####错误多，未能运行，设置变量太多，不想改了


import json
print("欢迎进入三乐购物")
print("输入s,进入登录。输入l开始注册")
sign_form = {}
self_fruitform = {}
#'self_money' = 0
while 1:

    choice = input("")
    a=login_sign(choice)
    fruit_form = {"f0001": {"name": "苹果", "price": 5},
                  "f0002": {"name": "香蕉", "price": 4},
                  "f0003": {"name": "车厘子", "price": 9},
                  "f0004": {"name": "羽毛球拍", "price": 50},
                  "f0005": {"name": "足球", "price": 49},
                  "f0006": {"name": "篮球", "price": 50},
                  "f0007": {"name": "羽毛球", "price": 4},
                  "f0008": {"name": "水壶", "price": 20},
                  }
    # for k,v in d1.items():
    #     print(f"{k} --> {v}")
    if a == 1:
        print("下面是商品价格表")
        for k,v in fruit_form.items():
            print(f"标签为{k}的水果及其价格为{v}")
        print("输入go开始购物,输入exit返回登录")
        go_exit = input("")
        return_1 = go_shopping(go_exit)
        if return_1 == 0:
            break
        else:    #return_1 == 1
            print(self_fruitform)  #打印购物车的内容
            print(f"你现在的余额为 {(price_sum)-(login_info[name]['self_money'])}")
            break


    def login_sign(choice):

        if choice == "s":
            sign_info={}

            name = ("请输入用户名")
            password = ("请输入密码")
            if name in sign_form:
                print("登录成功")
                return 1


            else:
                print("用户名或密码错误，请重新输入l或s")
                return 0

        elif choice == "l":
            login_info={}
            name =input ("请注册输入用户名")
            password =input ("请输入注册密码")
            login_info[name] = {"password":password, "self_money":0}

            return 0

        else:
            print("请正确输入")
            return 0


    def go_shopping(go_exit):
        if go_exit == "exit":
            return 0
        elif go_exit =="go":
           # {stuifo[name]['hobby']}
            print(f"你当前余额为{login_info['name']['self_money']}")#根据字典获取到用户的余额
            print("请输入商品编号加入购物车")
            return2 = shoping_cart()
            if return2==2 or return2==0:
               print("请重新进入系统选择")
               return 0
            else:
                return 1




    def shoping_cart():
# for k,v in d1.items():
#     print(f"{k}-->{v}")#使用两个变量去接收
        while 1:
            fruit_label = input("")
            if fruit_label == 'exit':
                return 2
            elif fruit_label in fruit_form.value():
                for fruit_label in fruit_form:
                    price1 = fruit_form[fruit_label]['price']
                    name = fruit_label
                self_fruitform[name] = {'price1':price1}
                i = 0
                a = []
                a[i] = int(price1)
                i += 1
                price_sum = sum(a)

                if price_sum > login_info[name]['self_money']:
                    print("余额不足")
                    return 0
                else :
                    return 1

            else:
                print("请正确输入编号")
                continue







